package com.sam.designpatterns


fun main() {
    val leader = Handler("直接上级", 1f)
    val manager = Handler("经理", 30f)
    val generalManager = Handler("总经理", 365f)
    val boss = FinalHandler("老板")

    //组链
    leader.next = manager
    manager.next = generalManager
    generalManager.next = boss

    leader.handRequest(Request(1f, "头疼脑热")) //脑热得隔离观察哈
    println("-------------")
    leader.handRequest(Request(14f, "老婆生娃"))
    println("-------------")
    leader.handRequest(Request(365f, "世界辣么大，我想去看看"))
    println("-------------")
    leader.handRequest(Request(3650f, "不想上班"))
}

open class Handler(
        val name: String, //处理者名字
        val leaveLimit: Float //能审批的最长时间
){
    var next: Handler? = null

    open fun handRequest(request: Request) {
        if (request.leaveDay <= leaveLimit) {
            println("${request.msg} -> 请假，$name 准了")
        } else {
            println("$name：超过${leaveLimit}天，让领导处理")
            next?.handRequest(request)
        }
    }
}

class FinalHandler(name: String) : Handler(name, Float.MAX_VALUE) {
    override fun handRequest(request: Request) {
        println("$name：${request.msg} -> 找财务领下工资，不用来了！")
    }
}

data class Request(
        val leaveDay: Float, //请假时长
        val msg: String //请假原因
)
